package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Myuser;
import model.Target;

import ejb.CategoryFacadeEJBRemote;
import ejb.TargetFacadeEJBRemote;
import ejb.UserFacadeEJBRemote;


@WebServlet("/MyMerchant")
public class MyMerchant extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	@EJB TargetFacadeEJBRemote tf;
	@EJB  UserFacadeEJBRemote uf;
	@EJB CategoryFacadeEJBRemote cf;
       
    public MyMerchant() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		PrintWriter out = response.getWriter();
		HttpSession session=request.getSession();
        String username=(String)session.getAttribute("username");
        try{
        if(username==null){
            out.print("Please login first! redirecting to Login...");
            response.setHeader("Refresh","2;URL=login.jsp");
        }
        else{
        	Myuser user=uf.findUserByName(username);

	        List<Target> merchant = tf.getAllTargetsMerchantByOne(user);
	        out.println("asdf"+ username);

	        out.println("size is: " + merchant.size());
	        
			request.setAttribute("merchantlist", merchant);	
        }
	        }catch(Exception e){
	        	e.printStackTrace();
	        }
	        finally{
	        	request.getRequestDispatcher("mymerchant.jsp").forward(request, response);
	        }
		
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
